﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using JohnnySins.DAL;
using JohnnySins.DAL.Repositories;
using JohnnySins.Entities;
using JohnnySins.Code;

namespace JohnnySins.Admin
{
    public partial class StarProfileList : System.Web.UI.Page
    {
        public string JsCodeToRunOnReady { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            FindSelected();

            if (!IsPostBack)
            {
                LoadStarProfile();
            }
        }
        public int[] Selected { get; set; }

        private void FindSelected()
        {
            var selected = new List<int>();

            foreach (GridViewRow row in grid.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    /*var check = row.Cells[0].FindControl("cbSelected") as CheckBox;
                    if (check.Checked)
                    {
                        var id = row.FindControl("hidId") as HiddenField;
                        selected.Add(int.Parse(id.Value));
                    }*/
                }
            }

            Selected = selected.ToArray();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            LoadStarProfile();
        }

        protected void LoadStarProfile()
        {
            using (var repo = RepositoryFactory.ResolveRepository<StarProfileRepository>())
            {
                grid.DataSource = repo.Search(txtNameFilter.Text, ddlGenderFilter.SelectedValue, null);
                grid.DataBind();
            }
        }

        protected void grid_RowDataBound(Object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                StarProfile item = (StarProfile)e.Row.DataItem;

                ImageButton ibtn = (ImageButton)e.Row.FindControl("btnDeactivate");
                ibtn.Visible = false;//item.IsActive;

                ibtn = (ImageButton)e.Row.FindControl("btnActivate");
                ibtn.Visible = false;//!item.IsActive;

                //CheckBox cbSelected = (CheckBox)e.Row.FindControl("cbSelected");
                //cbSelected.Checked = Selected.Contains(item.StarProfileId);
            }

            /*if (e.Row.RowType == DataControlRowType.Header)
            {
                CheckBox chbAll = (CheckBox)e.Row.FindControl("chbAll");
                chbAll.Checked = (Selected.Length == (grid.DataSource as IEnumerable<StarProfile>).Count());
            }*/
        }

        protected void grid_Command(object sender, CommandEventArgs e)
        {
            int id = Convert.ToInt32(e.CommandArgument.ToString());

            if (e.CommandName == "delete")
            {
                DeleteStarProfile(id);
            }
            else
            {
                /*using (var repo = RepositoryFactory.ResolveRepository<StarProfileRepository>())
                {
                    switch (e.CommandName)
                    {
                        case "deactivate":
                            repo.Deactivate(id);
                            break;
                        case "activate":
                            repo.Activate(id);
                            break;
                    }
                }*/
            }

            LoadStarProfile();
        }

        protected string GetFilePath(int id, string quality, string os)
        {
            return "";
            /*using (var repo = RepositoryFactory.ResolveRepository<StarProfileRepository>())
            {
                StarProfile video = repo.Find(id);

                string basePath = Server.MapPath("~/Files/StarProfiles/");

                if ((quality == "") && (os == ""))
                    return Path.Combine(basePath, "Preview", video.PhysFileName);

                if ((quality == "poster") && (os == ""))
                    return Path.Combine(basePath, "Poster", video.PhysPoster);

                if (quality == "hd")
                {
                    if (os == "qt")
                    {
                        return Path.Combine(basePath, "HDQT", video.PhysHDQTFileName);
                    }
                    else
                    {
                        return Path.Combine(basePath, "HDWin", video.PhysHDWinFileName);
                    }
                }
                else
                {
                    if (os == "qt")
                    {
                        return Path.Combine(basePath, "LDQT", video.PhysLDQTFileName);
                    }
                    else
                    {
                        return Path.Combine(basePath, "LDWin", video.PhysLDWinFileName);
                    }
                }
            }*/
        }

        protected void DeleteStarProfileFile(int id, string quality, string os)
        {
            string path = GetFilePath(id, quality, os);
            if (File.Exists(path))
                try
                {
                    File.Delete(path);
                }
                catch (Exception ex) { }
        }

        protected void DeleteStarProfile(int id)
        {
            using (var repo = RepositoryFactory.ResolveRepository<StarProfileRepository>())
            {
                StarProfile itm = repo.Find(id);
                if (itm != null)
                {
                    //TODO delete files

                    repo.Delete(id);
                }
            }
        }

        protected void btnDeleteSelected_Click(object sender, EventArgs e)
        {
            using (var repo = RepositoryFactory.ResolveRepository<StarProfileRepository>())
            {
                foreach (int id in Selected)
                {
                    DeleteStarProfile(id);
                }
            }

            LoadStarProfile();
        }

        protected void btnActivateSelected_Click(object sender, EventArgs e)
        {
            using (var repo = RepositoryFactory.ResolveRepository<StarProfileRepository>())
            {
                foreach (int id in Selected)
                {
                    //repo.Activate(id);
                }
            }

            LoadStarProfile();
        }

        protected void btnDeactivateSelected_Click(object sender, EventArgs e)
        {
            using (var repo = RepositoryFactory.ResolveRepository<StarProfileRepository>())
            {
                foreach (int id in Selected)
                {
                    //repo.Deactivate(id);
                }
            }

            LoadStarProfile();
        }

        protected void btnReverseSelected_Click(object sender, EventArgs e)
        {
            var otherScenes = new List<int>();

            using (var repo = RepositoryFactory.ResolveRepository<StarProfileRepository>())
            {
                foreach (var scene in repo.FindAll())
                {
                    if (!Selected.Contains(scene.StarProfileId))
                    {
                        otherScenes.Add(scene.StarProfileId);
                    }
                }
            }

            Selected = otherScenes.ToArray();
            LoadStarProfile();
        }

        protected void chbAll_CheckChanged(Object sender, EventArgs e)
        {
            bool chk = (sender as CheckBox).Checked;
            if (chk)
            {
                var other = new List<int>();
                using (var repo = RepositoryFactory.ResolveRepository<StarProfileRepository>())
                {
                    foreach (var scene in repo.FindAll())
                        other.Add(scene.StarProfileId);
                }

                Selected = other.ToArray();
            }
            else
                Selected = new int[] { };

            LoadStarProfile();
        }

        protected void btnAddProfile_Click(object sender, EventArgs e)
        {
            Response.Redirect("StarProfile.aspx");
        }
    }
}